import config from '../../config'
import store from '../../../store'
const baseUrl = process.env.NODE_ENV === 'development' ? config.baseUrl.dev : config.baseUrl.pro
const port = process.env.NODE_ENV === 'development' ? config.port.dev : config.port.pro
/**
 *
 * @param params 参数 eg: {flag: xxx}
 * @param name 导出文件名称
 * @param url 接口url
 */
export const exportSourse = (url , name, params='') => {
    let xhr = new XMLHttpRequest()
	let all_url = baseUrl + port + url
	if(params) {
		let get_params = '?'
		for(let key in params) {
			get_params += key+'='+params[key]+'&'
		}
		all_url = baseUrl + port + url + get_params
	}
	xhr.open('GET', all_url, true)
    xhr.responseType = 'blob'
    xhr.setRequestHeader('Content-Type', 'application/json;charset=utf-8')
    xhr.setRequestHeader('Authorization', store.state.user.token)
    xhr.onload = function () {
        if (this.status === 200) {
            let blob = this.response
            let a = document.createElement('a')
            let a_url = window.URL.createObjectURL(blob)
            a.href = a_url
            a.download = name
            a.click()
            a.remove()
        }
    }
    xhr.send()
}